#include <cstdint>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

class Solution {
    public:
	int minimumTime(int n, vector<vector<int>> &relations, vector<int> &time)
	{
		vector<vector<int>> gh(n + 1);
		vector<int> indgree(n + 1);
		vector<int> finish(n + 1);

		for (auto &v : relations) {
			gh[v[0]].push_back(v[1]);
			indgree[v[1]]++;
		}

		queue<int> q;
		for (int i = 1; i < indgree.size(); i++) {
			if (indgree[i] == 0) {
				q.push(i);
				finish[i] = time[i - 1];
			}
		}

		int ans = 0;
		while (!q.empty()) {
			int u = q.front();
			q.pop();
			if (gh[u].size() == 0) {
				ans = max(ans, finish[u]);
			}
			//ans = max(ans, finish[u]);
			for (auto v : gh[u]) {
				//finish[v] = max(finish[v], finish[u] + time[v - 1]);
				if (finish[v] < finish[u] + time[v - 1]) {
					finish[v] = finish[u] + time[v - 1];
				}
				if (--indgree[v] == 0) {
					q.push(v);
				}
			}
		}
		return ans;
	}
};

int main()
{
	int n = 3;
	vector<vector<int>> relations = { { 1, 3 }, { 2, 3 } };
	vector<int> time = { 3, 2, 5 };
	cout << Solution().minimumTime(n, relations, time) << endl;
	return 0;
}